package com.toe.business.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.business.service.BusinessInformationService;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.dto.business.BusinessInfoUpdateDTO;
import com.toe.pojo.entity.business.BusinessInformation;
import com.toe.business.webapi.mapper.BusinessInformationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.pojo.vo.business.BusinessInfoDetailsVO;
import com.toe.pojo.vo.business.BusinessInfoId;
import com.toe.pojo.vo.business.BusinessInfoShowVO;
import com.toe.pojo.vo.recommend.RecommendBusinessVO;
import com.toe.pojo.vo.user.UserUserVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@DubboService
public class BusinessInformationServiceImpl extends ServiceImpl<BusinessInformationMapper, BusinessInformation>
        implements BusinessInformationService {

    @Autowired
    BusinessInformationMapper businessInformationMapper;

    /**
     * 通过ids对比map_id,返回健身房信息(RecommendBusinessVO)
     * @param ids
     */
    public List<RecommendBusinessVO> businessByIds(List<String> ids) {
        System.out.println("========商家输出==========");
        ids.forEach(System.out::println);
        List<RecommendBusinessVO> recommendBusinessVOS =
                businessInformationMapper.ListByIds(ids);
        System.out.println("========查询到的商家========");
        recommendBusinessVOS.forEach(System.out::println);

//        if (recommendBusinessVOS.isEmpty()) {
//            throw new ToeServiceException(
//                    StateEnum.ERR_SERVER_ERROR, "服务器忙,请稍后重试");
//        }
        return recommendBusinessVOS;
    }

    /**
     * 根据商家id查询商家详细信息
     * @param id
     * @return
     */
    @Override
    public BusinessInfoDetailsVO businessInfoDetails(Long id) {
        BusinessInfoDetailsVO detailsVOList =
                businessInformationMapper.businessInfoDetails(id);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>detailsVOList: " + detailsVOList);
        return detailsVOList;
    }

    @Override
    public List<String> businessCover(Long id) {
        List<String> coverUrl = businessInformationMapper.businessCover(id);
        System.out.println("封面 = " + coverUrl);
        return coverUrl;
    }

    /**
     * 返回商家id和name
     * @return
     */
    @Override
    public List<BusinessInfoId> businessIds() {
        List<BusinessInfoId> ids = businessInformationMapper.businessIds();
        return ids;
    }


    /**
     * 商家修改个人信息
     * @param businessInfoUpdateDTO
     */
    @Override
    public void updateBusinessInfo(BusinessInfoUpdateDTO businessInfoUpdateDTO) {
        businessInformationMapper.updateBusinessInfoById(businessInfoUpdateDTO);
    }

    /**
     * 商家信息的分页
     * @param pageNum 要查询的页码
     * @param pageSize 每页条数
     * @return
     */
    public JsonPage<BusinessInfoShowVO> getAllBusinessByPage(Integer pageNum, Integer pageSize){
        System.out.println("pageNum: "+pageNum+"  pageSize: "+pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<BusinessInfoShowVO> list = businessInformationMapper.findAllBusiness();
        return JsonPage.restPage(new PageInfo<>(list));
    }


    // 分页查询入驻商家列表
    @Override
    public JsonPage<BusinessInformation> getBusinessListByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BusinessInformation> list = businessInformationMapper.queryBussinessList();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    // 根据id逻辑删除商家
    @Override
    public void removeBusinessInformation(Long id) {
        BusinessInformation businessInformation=new BusinessInformation();
        businessInformation.setId(id);
        businessInformationMapper.deleteById(businessInformation);
    }

}
